import MailApi from './service.js'
import Detail from './detail/index.vue'
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'

export default {
  data () {
    return {
      detailShow: false,
      loading: false,
      ids: '',
      searchForm: {
        Theme: '', // 邮件主题
        FromName: '', // 发件人姓名
        BeginTime: '',
        EndTime: '',
        Status: '',
        PageIndex: 1,
        PageSize: 10,
      },
      
      totalRows: 0,
      readCount: 0, // 已读
      unreadCount: 0, // 未读
      tableData: [],
      detailData: [],
      replyShow: true
    }
  },
  components: {
    Detail
  },
  mounted () {
  },
  computed: {
    // actionArr () {
    //   let arr = this.$store.state.user.actionAccess.map(item => {
    //     return item.code
    //   })
    //   return arr
    // },
    // hasFind () {
    //   return this.actionArr.indexOf('Find') > -1
    // },
    // hasExport () {
    //   return this.actionArr.indexOf('Export') > -1
    // }
  },
  methods: {
    ...mapActions([
      'handleInbox',
      'handleOutbox'
    ]),
    // 获取列表
    getList (type) {
      MailApi.getMailList(Object.assign({SearchType: type},this.searchForm)).then(res => {
        if (res.data.success) {
          this.tableData = res.data.data
          this.totalRows = res.data.totalRows
          this.readCount = res.data.readCount
          this.unreadCount = res.data.unreadCount
        } else {
          this.$Message.error(res.data.message)
        }
      })
    },
    // 表格change
    onSelectChange (val) {
      console.log(val)
      let arr = val.map(item => {
        return item.id
      })
      if (arr.length > 0) {
        this.Ids = arr.join(',')
      } else {
        this.Ids = ''
      }
    },
    dateChange (val) {
      console.log(val)
      if (val) {
        this.searchForm.BeginTime = val[0]
        this.searchForm.EndTime = val[1]
      } else {
        this.searchForm.BeginTime = ''
        this.searchForm.EndTime = ''
      }
    },
    // 详情
    gotoDetail (type,id) {
      this.loading = true
      this.$refs.detail_info.openModal()
      if (type === 3) this.replyShow = false
      else this.replyShow = true
      MailApi.getDetail({
        type: type,
        id: id
      }).then(res => {
        console.log(res.data)
        if (res.data.success) {
          this.loading = false
          this.detailData = res.data.data
          this.getList(type)
        } else {
          this.$Message.error(res.data.message)
        }
      })
    },

    // 邮件回收
    DeleteMail (type) {
      if (this.Ids) {
        this.$Modal.confirm({
          title: "提示",
          content: "<p>确定将邮件放入回收站？</p>",
          onOk: () => {
            MailApi.DeleteMail(type, {
              id: this.Ids
            }).then(res => {
              console.log(res)
              if (res.data.success) {
                this.$Message.success(res.data.message)
                this.getList(type)
              } else {
                this.$Message.error({
                  content: res.data.message,
                  duration: 5,
                  closable: true
                });
              }
            })    
          },
          onCancel: () => {}
        });
      } else {
        this.$Message.warning('请选择需要回收的邮件')
      }
      
    },
    /**
     * 分页
     * @param {Number} val pageIndex
     */
    onChangePage (val) {
      this.searchForm.PageIndex = val
      this.getList()
    },
    /**
     * 分页页码变动
     */
    onpageSizeChange (val) {
      this.searchForm.PageIndex = 1
      this.searchForm.PageSize = val
      this.getList()
    },
    
  }
}